package com.zsy.controller;

import com.zsy.entity.PurchaseRequisition;
import com.zsy.service.IPurchaseRequisitionsService;
import com.zsy.utils.PageBean;
import com.zsy.utils.Result;
import com.zsy.utils.Result02;
//import com.zsy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//集成了ResponseBody；当前的方法返回的页面是纯文本
@RestController
@RequestMapping("/PurchaseRequisition")
//跨域问题解决方案
@CrossOrigin
public class PurchaseRequisitionController {

    //注入service
    @Autowired
    private IPurchaseRequisitionsService PRS;
    @PostMapping("/login")
    public String login(String user){
        System.out.println(user);
        return "login";
    }

    /**
     * 查询所有采购申请
     * @param pageNum 页码
     * @param pageSize 每页显示条数
     * @param purchaseId 采购申请编号
     * @param relevantData 相关数据
     * @param supplier 供应商
     * @return json字符串
     */
    @GetMapping("/list")
    public Result02<PageBean<PurchaseRequisition>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String purchaseId,
            @RequestParam(required = false) String relevantData,
            @RequestParam(required = false) String supplier) {

        //权限
//        Map<String,Object> map = ThreadLocalUtil.get();
//        String department = (String) map.get("department");
//        if(department.equals("采购部") || department.equals("管理员")){
            System.out.println(">>>>purchaseId:"+purchaseId +"relevantData:"+ relevantData + "supplier:"+supplier);

            if ("".equals(purchaseId) ){
                purchaseId = null;
            }
            if ("".equals(relevantData)){
                relevantData = null;
            }
            if ("".equals(supplier)){
                supplier = null;
            }
            PageBean<PurchaseRequisition> list = PRS.list(pageNum, pageSize, purchaseId, relevantData, supplier);
            return Result02.success(list);
//        }
//        else {
//            return Result02.error("错误");
//        }

    }

    /**
     * 新增采购申请
     * @param purchaseRequisition 采购申请
     * @return json字符串
     */
    @PostMapping("/add")
    public Result add(@RequestBody PurchaseRequisition purchaseRequisition) {
        System.out.println(purchaseRequisition.toString());
        return PRS.add(purchaseRequisition);
    }

    @PutMapping("/delete")
    public Result delete(Integer id) {
        System.out.println(id);
        return PRS.deleteById(id);
    }

    @PostMapping("/update")
    public Result updata(@RequestBody @Validated PurchaseRequisition purchaseRequisition) {
        System.out.println("controller:>>"+purchaseRequisition.toString());
        return PRS.updateById(purchaseRequisition);
    }


}
